package org.dromara.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.wms.domain.WmsOrderLine;
import org.dromara.wms.domain.WmsWarehouse;
import org.dromara.wms.domain.WmsWarehouseProduct;
import org.dromara.wms.service.WmsWarehouseProductServiceImpl;
import org.dromara.wms.service.WmsWarehouseServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库管理
 *
 * @author wenrui
 * @date 2024-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/warehouse")
public class WmsWarehouseController extends BaseService {

  private final WmsWarehouseServiceImpl wmsWarehouseService;
  private final WmsWarehouseProductServiceImpl wmsWarehouseProductService;

  /**
   * 查询仓库管理列表
   */
  @SaCheckPermission("wms:warehouse:list")
  @GetMapping("/list")
  public R<List<WmsWarehouse>> list(WmsWarehouse bo) {
    return R.ok(wmsWarehouseService.queryList(bo));
  }

  /**
   * 导出仓库管理列表
   */
  @SaCheckPermission("wms:warehouse:export")
  @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(WmsWarehouse bo, HttpServletResponse response) {
    List<WmsWarehouse> list = wmsWarehouseService.queryList(bo);
    ExcelUtil.exportExcel(list, "仓库管理", WmsWarehouse.class, response);
  }

  /**
   * 获取仓库管理详细信息
   *
   * @param id 主键
   */
  @SaCheckPermission("wms:warehouse:query")
  @GetMapping("/{id}")
  public R<WmsWarehouse> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
    return R.ok(wmsWarehouseService.queryById(id));
  }

  /**
   * 新增仓库管理
   */
  @SaCheckPermission("wms:warehouse:add")
  @Log(title = "仓库管理", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public R<Void> add(@Validated(AddGroup.class) @RequestBody WmsWarehouse bo) {
    return toAjax(wmsWarehouseService.insertByBo(bo));
  }

  /**
   * 修改仓库管理
   */
  @SaCheckPermission("wms:warehouse:edit")
  @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public R<Void> edit(@Validated(EditGroup.class) @RequestBody WmsWarehouse bo) {
    return toAjax(wmsWarehouseService.updateByBo(bo));
  }

  /**
   * 删除仓库管理
   *
   * @param ids 主键串
   */
  @SaCheckPermission("wms:warehouse:remove")
  @Log(title = "仓库管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public R<Void> remove(@NotEmpty(message = "主键不能为空")
                        @PathVariable Long[] ids) {
    return toAjax(wmsWarehouseService.deleteWithValidByIds(List.of(ids), true));
  }

  /* -----------仓库预警----------- */

  /**
   * 查询仓库管理列表
   */
  @SaCheckPermission("wms:warehouseProduct:list")
  @GetMapping("/listWWP")
  public TableDataInfo<WmsWarehouseProduct> list(WmsWarehouseProduct bo, PageQuery pageQuery) {
    return wmsWarehouseProductService.queryList(bo,pageQuery);
  }

  /**
   * 新增仓库预警
   */
  @SaCheckPermission("wms:warehouseProduct:add")
  @Log(title = "仓库管理", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping("/addWWP")
  public R<Void> addWWP(@Validated(AddGroup.class) @RequestBody WmsWarehouseProduct[] bo) {
    return toAjax(wmsWarehouseProductService.insertByBo(bo));
  }

  /**
   * 删除仓库预警
   *
   */
  @SaCheckPermission("wms:warehouseProduct:remove")
  @Log(title = "仓库管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deleteWWP")
  public R<Void> remove(@RequestBody WmsWarehouseProduct bo) {
    return toAjax(wmsWarehouseProductService.deleteById(bo));
  }

//  /**
//   * 修改仓库预警
//   */
//  @SaCheckPermission("wms:warehouseProduct:edit")
//  @Log(title = "修改仓库预警", businessType = BusinessType.UPDATE)
//  @RepeatSubmit()
//  @PutMapping("/wwp")
//  public R<Void> edit(@Validated(EditGroup.class) @RequestBody WmsWarehouseProduct[] bo) {
//    return toAjax(wmsWarehouseProductService.updateByBo(bo));
//  }
}
